package com.ruoyi.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

/**
 * @Description: TODO
 * @Create by: jacob
 * @Date: 2025/1/24
 * 遇见未来的自己
 */
public class WxConfig {
    public static final String SP_APPID = "wxbfab58e7fb31d79c"; // 公众号ID
    public static final String SP_SECRET = "51244749c0925356f01a884f9479c6d2"; // 公众号密钥
    public static final String SP_MCHID = "1722441249"; // 商户号ID
    /** 商户API私钥路径 */
//    public static String PRIVATE_KEY_PATH = "/app/cert/apiclient_key.pem";
    public static String PRIVATE_KEY_PATH = "/Users/mac/app/cert/apiclient_key.pem";
    /** 商户证书序列号 */
    public static String MERCHANT_SERIAL_NUMBER = "38B200CA5E52334DC838F933626C0CFC34864E7B";
    /** 商户API公钥路径 */
//    public static String PUBLIC_KEY_PATH = "/app/cert/pub_key.pem";
    public static String PUBLIC_KEY_PATH = "/Users/mac/app/cert/pub_key.pem";
    /** 商户API公钥ID */
    public static String PUBLIC_KEY_ID = "PUB_KEY_ID_0117224412492025072000181880000401";
    /** 商户APIV3密钥 */
    public static String API_V3_KEY = "51244749c0925356f01a884f9479c6d2";


    /**
     * 签名规则
     */
    public static final String SIGN_RULE = "jsapi_ticket=%s&noncestr=%s&timestamp=%d&url=%s";

    /**
     * 微信支付统一回调路径
     */
    public static final String WX_PAY_CALLBACK_URL = "http://skmtest.xinyueyun.vip/skmapp/skmWxPay/wxPayCallback";





    public static String sign(String url, String nonce, Long timestamp, String ticket) throws Exception {
        String plain = String.format(SIGN_RULE, ticket, nonce, timestamp, url);
        try {
            MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
//            sha1.reset();
            byte[] digest = sha1.digest(plain.getBytes());
            return bytesToHex(digest);
        } catch (NoSuchAlgorithmException e) {
            // throw new LeysenException("jsapi_ticket计算签名错误");
            throw new Exception("jsapi_ticket计算签名错误");
        }
    }

    public static String bytesToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }



}
